//
//  GetMessageListVm.swift
//  Pods
//
//  Created by eme on 2017/4/10.
//
//

import UIKit
import UtilCore
import RxSwift
import SwiftyJSON
import ModelProtocol
import RickonCore
import NetWorkCore

class GetMessageListVm: Base_Vm {
    
    let backDate = Variable<[RowsModel]>([])
    
    let refreshing = Variable(false)
    
    let page = Variable(1)
    
    let hasNextPage = Variable(true)
    
    
    var messageType = "2"
    
    var message_Type = PublishSubject<String>()
    
    var loading:Observable<Bool>
    
    init (
        input:(
        refreshTriger:Observable<Void>,
        loadMoreTriger:Observable<Void>
        )
        ){
        let requestIndicator = ActivityIndicator()
        self.loading = requestIndicator.asObservable()
        
        super.init()
        let refreshRequest = Observable.of(
            input.refreshTriger,
            reloadTrigger.withLatestFrom(input.refreshTriger)).merge().map{ _ in
                
                return MineApi.getMessageList(type: self.messageType, begin: 0, length: AppConfig.pageSize)
                
            }.shareReplay(1)
        
        let refreshResponse = refreshRequest.emeRequestApiForObj(MessageModel.self,activityIndicator:requestIndicator).shareReplay(1)
        
        
        refreshResponse.subscribe(onNext:{[unowned self] result in
            switch result{
            case .success(let data):
                
                self.hasNextPage.value = data.rows.count == AppConfig.pageSize
                self.backDate.value = data.rows
                self.page.value = 1
                
            case .failure(let error):
                
                self.refresherror.onNext(error)
                
            }
        }).addDisposableTo(disposeBag)
        
        
        let loadMoreRequest = (
            input.loadMoreTriger
            ).map{ _ in
                
                return MineApi.getMessageList(type: self.messageType, begin: self.page.value, length: AppConfig.pageSize)
                
            }.shareReplay(1)
        
        let loadMoreResponse = loadMoreRequest.emeRequestApiForObj(MessageModel.self).shareReplay(1)
        
        loadMoreResponse.subscribe(onNext:{[unowned self] result in
            switch result {
            case .success(let data):
                
                self.hasNextPage.value = data.rows.count == AppConfig.pageSize
                self.backDate.value += data.rows
                self.page.value += 1
                
            case .failure(let error):
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        refreshRequest.subscribe(onNext:{ _ in
            
            self.hasNextPage.value = true
            self.page.value = 1
            
            }).addDisposableTo(disposeBag)
        
        
        
        
        let request = Observable.of(
            message_Type.asObservable(),
            reloadTrigger.withLatestFrom(message_Type.asObservable())).merge().map{ type in
                
                return MineApi.getMessageList(type: type, begin: 0, length: AppConfig.pageSize)
                
            }.shareReplay(1)
        
        let response = request.emeRequestApiForObj(MessageModel.self, activityIndicator: requestIndicator).shareReplay(1)
        
        response.subscribe(onNext:{[unowned self] result in
            switch result{
            case.success(let data):
                
                self.hasNextPage.value = data.rows.count == AppConfig.pageSize
                self.backDate.value = data.rows
                
            case .failure(let error):
                
                self.refresherror.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        request.subscribe(onNext:{ _ in
            
            self.hasNextPage.value = true
            self.page.value = 1
            
            }).addDisposableTo(disposeBag)
        
    }
    
}
